//
//  BaseRequestPlugin.swift
//  BaseComponent
//
//  Created by zhang qiang on 2018/11/21.
//  Copyright © 2018年 张强. All rights reserved.
//

import UIKit
import Foundation
import Moya
import Result

public class MTTokenInvPlugin: NSObject, PluginType {
    
    public func willSend(_ request: RequestType, target: TargetType) {
        
    }
    
    public func didReceive(_ result: Result<Moya.Response, MoyaError>, target: TargetType) {
        switch result {
        case .success(let response):
            // 检查各种code 3 为 token 呗挤掉线 ,  10000 也有可能是 , 需要后台更改
            if let data = try? response.mapEmptyModel(), let code = data.code, code == 510 {
                // Token 失效处理
                UserCenter.default.logout()
            }
            
        default:
            break
        }
    }
}
